package com.usian.controller;

import com.usian.comment.Comment;
import com.usian.feedback.Feedback;
import com.usian.service.SquareService;
import com.usian.square.Square;

import com.usian.user.User;
import com.usian.utils.AliyunOSSUtils;
import com.usian.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/square")
public class SquareController {
    @Autowired
    private SquareService squareService;
    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @RequestMapping("/findAll")
    public List<Square> findAll(@RequestBody Square square){
        return squareService.findAll(square);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Square square){
        square.setComment("0");
        square.setPraise("0");
        squareService.add(square);
        return Result.success();
    }

    @RequestMapping("/findById")
    public Result findById(@RequestParam("userId") Integer userId){
        List<Square> list = squareService.findById(userId);
        User user = squareService.selectUserByIdTwo(userId);
        for (Square square : list) {
            square.setName(user.getName());
            square.setImg(user.getImg());
            square.setIntroduction(user.getIntroduction());
        }
        return Result.success(list);
    }

    @GetMapping("/comment/{id}/{status}")
    public Result comment(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        List<Comment> commentList = squareService.comments(id,status);
        for (Comment comment : commentList) {
            User user = squareService.selectUserThree(comment.getUserId());
            if (user!=null){
                comment.setName(user.getName());
                comment.setImg(user.getImg());
            }
        }
        return Result.success(commentList);
    }

    @PostMapping("/addComment")
    public Result addComment(@RequestBody Comment comment){
        squareService.addComment(comment);
        return Result.success();
    }

    @PostMapping("/praise")
    public Result praise(@RequestBody Square square){
        int updatePraise = squareService.praise(square.getId());
        return Result.success(updatePraise);
    }

    @RequestMapping("/comment")
    public List<Map<String,Object>> comment(){
        return squareService.comment();
    }

    @PostMapping("/feedback")
    public Result feedback(@RequestBody Feedback feedback){
        squareService.feedback(feedback);
        return Result.success();
    }

    @PostMapping("/upload")
    public Result upload(@RequestParam("image") MultipartFile file) throws IOException {
        // 获取文件名
        String filename = file.getOriginalFilename();
        // 获取io流
        InputStream inputStream = file.getInputStream();
        // 文件上传
        String fileUtils = aliyunOSSUtils.putObject(inputStream, filename);
        return Result.success(fileUtils);
    }

}
